const app = getApp();
import { getPhoneNumber } from "../../services/login.js"
Page({
  data: {
    bg_video: 'https://static.rbanma.com/liuhu/qkmingdaoapp/assets/videos/bg_video_2.mp4',
    bg_image: 'https://static.rbanma.com/liuhu/qkmingdaoapp/assets/imgs/index/bg_img_2.jpg',
    isMuted: false,//是否静音
    isShowSubtitle: true,//是否显示字幕
    isSkipSubtitle: false,//是否跳过字幕
    isShowVoide: false,//是否显示视频
    subtitles: [{//字幕组
        start: 0,
        end: 3,
        text: '欢迎您体验非遗乾坤手诊'
      },
      {
        start: 3,
        end: 9,
        text: '我们的双手是身体健康状况\n的晴雨表和病理档案'
      },
      {
        start: 9,
        end: 15,
        text: '可以诊断过去、现在\n和未来的健康状况'
      },
      {
        start: 15,
        end: 23,
        text: '乾坤手诊可以立体、多元\n多维度、多角度诊断人体疾病'
      },
      {
        start: 23,
        end: 27,
        text: '可以做到未病先知，天人合一'
      },
      {
        start: 27,
        end: 30,
        text: '可以为人类的健康提供帮助'
      },
      {
        start: 30,
        end: 34,
        text: '请您根据如下步骤\n提供您的手部信息'
      }
    ],
  },
  onLoad() {
    // 招牌顶部与胶囊顶部对齐
    this.setData({
      wrapperTop: app.globalData.menuBtnTop,
      isMuted: wx.getStorageSync('isMuted'),
      mobile: wx.getStorageSync('mobile')
    })
    this.videoContext = wx.createVideoContext('bg_video');
  },
  // 重新播放视频
  replayVideo() {
    this.videoContext.seek(0);
    this.videoContext.play(); // 开始播放
    this.setData({
      isSkipSubtitle: false
    });
  },
  // 视频加载完成
  onVideoLoadedMetadata(e) {
    this.videoContext.seek(app.globalData.videoDuration-0.1)
    this.videoContext.play(); // 开始播放
    app.globalData.videoDuration= e.detail.duration //播放总时长
    if(wx.getStorageSync('operate_step')) {
      this.videoContext.seek(app.globalData.videoDuration-0.1)
      this.skipVideo()
    }
    this.setData({ 
      isSkipSubtitle:false,//是否跳过字幕
      isShowVoide: true//是否显示视频
    })
  },
  // 暂停播放触发
  onVideoPause() {
    this.videoContext.seek(app.globalData.videoDuration-0.1)
    this.skipVideo()
  },
  //跳过按钮 隐藏字幕
  skipVideo() {
    this.setData({
      isSkipSubtitle: true,
      currentSubtitle: null,
      isShowSubtitle: false
    });
  },
  // 静音
  toggleMute() {
    wx.setStorageSync('isMuted', !this.data.isMuted),
    this.setData({
      isMuted: wx.getStorageSync('isMuted')
    })
  },
  onVideoTimeUpdate: function (e) {
    if (this.data.isSkipSubtitle) return
    const currentTime = e.detail.currentTime;
    const subtitles = this.data.subtitles;
    let currentSubtitle = null;
    for (let i = 0; i < subtitles.length; i++) {
      const subtitle = subtitles[i];
      if (currentTime >= subtitle.start && currentTime < subtitle.end) {
        currentSubtitle = subtitle;
        break;
      }
    }
    if (currentSubtitle !== this.data.currentSubtitle) {
      this.setData({
        currentSubtitle: currentSubtitle,
        currentSubtitleText: currentSubtitle && currentSubtitle.text,
        isShowSubtitle: !!currentSubtitle
      });
    }
  },
  // 开始拍摄跳转
  navigateToCapturePage(e) {
    getPhoneNumber(e, (mobile)=>{
      this.setData({mobile})
      wx.navigateTo({
        url: '/pages/capture/capture'
      })
    })
  },

  // 手诊学堂页跳转
  navigateToHandDiagnosisPage: function () {
    wx.navigateTo({
      url: '/pages/hand-diagnosis/hand-diagnosis'
    })
  },
  // 我的(历史诊断页)跳转
  navigateToHealthReportsPage(e) {
    getPhoneNumber(e, (mobile)=>{
      this.setData({mobile})
      wx.navigateTo({
        url: '/pages/health-reports/health-reports'
      })
    })
  },
})